
<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>API修复测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }

        .test-section {
            margin: 20px 0;
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }

        .success {
            background-color: #d4edda;
            border-color: #c3e6cb;
            color: #155724;
        }

        .error {
            background-color: #f8d7da;
            border-color: #f5c6cb;
            color: #721c24;
        }

        button {
            background-color: #007bff;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
            margin: 5px;
        }

        button:hover {
            background-color: #0056b3;
        }

        pre {
            background-color: #f8f9fa;
            padding: 10px;
            border-radius: 5px;
            overflow-x: auto;
        }
    </style>
</head>

<body>
    <h1>API修复测试</h1>

    <div class="test-section">
        <h2>测试游戏列表API</h2>
        <button onclick="testGetGameList()">测试获取游戏列表</button>
        <div id="gameListResult"></div>
    </div>

    <div class="test-section">
        <h2>测试创建游戏API</h2>
        <button onclick="testCreateGame()">测试创建游戏</button>
        <div id="createGameResult"></div>
    </div>

    <script>
        const API_BASE_URL = 'http://localhost:5152/api';

        async function testGetGameList() {
            const resultDiv = document.getElementById('gameListResult');
            resultDiv.innerHTML = '<p>正在测试...</p>';

            try {
                const response = await fetch(`${API_BASE_URL}/game/list`);
                const data = await response.json();

                if (response.ok) {
                    resultDiv.className = 'test-section success';
                    resultDiv.innerHTML = `
                        <h3>✅ 成功！</h3>
                        <p>状态码: ${response.status}</p>
                        <p>返回数据:</p>
                        <pre>${JSON.stringify(data, null, 2)}</pre>
                    `;
                } else {
                    resultDiv.className = 'test-section error';
                    resultDiv.innerHTML = `
                        <h3>❌ 失败</h3>
                        <p>状态码: ${response.status}</p>
                        <p>错误信息:</p>
                        <pre>${JSON.stringify(data, null, 2)}</pre>
                    `;
                }
            } catch (error) {
                resultDiv.className = 'test-section error';
                resultDiv.innerHTML = `
                    <h3>❌ 网络错误</h3>
                    <p>错误信息: ${error.message}</p>
                `;
            }
        }

        async function testCreateGame() {
            const resultDiv = document.getElementById('createGameResult');
            resultDiv.innerHTML = '<p>正在测试...</p>';

            try {
                const gameData = {
                    name: "测试游戏",
                    maxPlayers: 4,
                    gameDuration: 180,
                    playerName: "测试玩家",
                    roomPassword: ""
                };

                const response = await fetch(`${API_BASE_URL}/game/create`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify(gameData)
                });

                const data = await response.json();

                if (response.ok) {
                    resultDiv.className = 'test-section success';
                    resultDiv.innerHTML = `
                        <h3>✅ 成功！</h3>
                        <p>状态码: ${response.status}</p>
                        <p>游戏创建成功:</p>
                        <pre>${JSON.stringify(data, null, 2)}</pre>
                    `;
                } else {
                    resultDiv.className = 'test-section error';
                    resultDiv.innerHTML = `
                        <h3>❌ 失败</h3>
                        <p>状态码: ${response.status}</p>
                        <p>错误信息:</p>
                        <pre>${JSON.stringify(data, null, 2)}</pre>
                    `;
                }
            } catch (error) {
                resultDiv.className = 'test-section error';
                resultDiv.innerHTML = `
                    <h3>❌ 网络错误</h3>
                    <p>错误信息: ${error.message}</p>
                `;
            }
        }
    </script>
</body>

</html>